//
//  BSKWeatherPicker.m
//  MicroMemory
//
//  Created by rimi on 16/7/15.
//  Copyright © 2016年 BlueSky. All rights reserved.
//

#import "BSKWeatherPicker.h"
#import <UIView+SDAutoLayout.h>
#import "BSKTheme.h"
#import "BSKWeatherTableViewCell.h"
#import "BSKconsts.h"

@interface BSKWeatherPicker ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong) UITableView * weatherTableView;
@property(nonatomic,strong) UIButton * curentWeatherButton;
@property(nonatomic,strong) NSArray * weatherImageNames;
@property(nonatomic,assign) BOOL isOpen;
@end


@implementation BSKWeatherPicker


+(NSString * )getWeatherImageNameWithType:(BSKWeatherType)type{
	NSArray * names = @[@"sun",@"雨",@"阴",@"多云",@"雷阵雨",@"雾",@"雪"];
	return names[type];
}

- (instancetype)init
{
	self = [super init];
	if (self) {
		self.frame = CGRectMake(ScreenWidth - 60, StatusBarHeight+NavigationBarHeight, 50, 50);
		self.backgroundColor = [UIColor clearColor];
		_isOpen = NO;

		_curentWeatherButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_curentWeatherButton setImage:[UIImage imageNamed:@"sun"] forState:UIControlStateNormal];
		[self addSubview:_curentWeatherButton];
		_curentWeatherButton.sd_layout
			.topSpaceToView(self,5)
			.leftSpaceToView(self,5)
			.rightSpaceToView(self,5)
			.heightEqualToWidth();
		_curentWeatherButton.layer.cornerRadius = 20;
		_curentWeatherButton.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5);
		CGFloat r=0,g=0,b=0,a=0;
		[[BSKTheme curentTheme].themeColor getRed:&r green:&g blue:&b alpha:&a];
		UIColor * theColor = [UIColor colorWithRed:r green:g blue:b alpha:0.6];
		_curentWeatherButton.backgroundColor = theColor;
		[_curentWeatherButton addTarget:self action:@selector(openWeatherMenuAction:) forControlEvents:UIControlEventTouchUpInside];

		_weatherImageNames = @[@"sun",@"雨",@"阴",@"多云",@"雷阵雨",@"雾",@"雪"];
		_weatherTableView = [[UITableView alloc]init];
		[self addSubview:_weatherTableView];
		_weatherTableView.rowHeight = 55;
		_weatherTableView.sd_layout
			.topSpaceToView(self,50)
			.leftSpaceToView(self,5)
			.rightSpaceToView(self,5)
			.bottomSpaceToView(self,0);
		_weatherTableView.backgroundColor = [UIColor clearColor];
		_weatherTableView.dataSource = self;
		_weatherTableView.delegate = self;
		_weatherTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
		self.layer.cornerRadius = 10;
	}
	return self;
}
#pragma mark Action
-(void)openWeatherMenuAction:(UIButton * )sender{
	if (_isOpen) {
		[UIView animateWithDuration:0.35 animations:^{
			self.frame = CGRectMake(ScreenWidth - 60, StatusBarHeight+NavigationBarHeight, 50, 50);
			self.weatherTableView.frame = CGRectMake(5, 50, 40, 0);
			self.backgroundColor = [UIColor clearColor];
	} completion:^(BOOL finished) {
		_isOpen = NO;
	}];
		
	}else{
	CGRect frame = self.frame;
	frame.size = CGSizeMake(50, 180);
	[UIView animateWithDuration:0.35 animations:^{
		self.frame = frame;
		self.weatherTableView.frame = CGRectMake(5, 50, 40, 120);
		self.backgroundColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:0.51];
	} completion:^(BOOL finished) {
		_isOpen = YES;
	}];
	}
}
#pragma mark TableViewDelegate

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
	return _weatherImageNames.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	BSKWeatherTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"weatherCell"];
	if (!cell) {
		cell = [[BSKWeatherTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"weatherCell"];
	}
	cell.weatherImage = [UIImage imageNamed:_weatherImageNames[indexPath.row]];
	return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	self.selectedWeatherType = (int)indexPath.row;
	[self openWeatherMenuAction:nil];
}

-(void)setSelectedWeatherType:(BSKWeatherType)selectedWeatherType{
	_selectedWeatherType = selectedWeatherType;
	[_curentWeatherButton setImage:[UIImage imageNamed:_weatherImageNames[selectedWeatherType]] forState:UIControlStateNormal];
}

@end
